// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 

package webserver;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.SocketException;
import java.net.URLDecoder;
import java.util.Date;

// Referenced classes of package org.jibble.simplewebserver:
//            SimpleWebServer

public class RequestThread extends Thread
{

    public RequestThread(Socket socket, File file)
    {
        _socket = socket;
        _rootDir = file;
    }

    private static void sendHeader(BufferedOutputStream bufferedoutputstream, int i, String s, long l, long l1)
        throws IOException
    {
        bufferedoutputstream.write(("HTTP/1.0 " + i + " OK\r\n" + "Date: " + (new Date()).toString() + "\r\n" + "Server: JibbleWebServer/1.0\r\n" + "Content-Type: " + s + "\r\n" + "Expires: Thu, 01 Dec 1994 16:00:00 GMT\r\n" + (l == -1L ? "" : "Content-Length: " + l + "\r\n") + "Last-modified: " + (new Date(l1)).toString() + "\r\n" + "\r\n").getBytes());
    }

    private static void sendError(BufferedOutputStream bufferedoutputstream, int i, String s)
        throws IOException
    {
        s = s + "<hr>" + "SimpleWebServer  http://www.jibble.org/";
        sendHeader(bufferedoutputstream, i, "text/html", s.length(), System.currentTimeMillis());
        bufferedoutputstream.write(s.getBytes());
        bufferedoutputstream.flush();
        bufferedoutputstream.close();
    }

    public void run()
    {
        BufferedInputStream bufferedinputstream = null;
        BufferedOutputStream bufferedoutputstream = null;
        String s = null;
        try {
			_socket.setSoTimeout(30000);
		
        BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(_socket.getInputStream()));
        bufferedoutputstream = new BufferedOutputStream(_socket.getOutputStream());
        s = bufferedreader.readLine();
        if(s == null || !s.startsWith("GET ") || !s.endsWith(" HTTP/1.0") && !s.endsWith("HTTP/1.1"))
        {
            sendError(bufferedoutputstream, 500, "Invalid Method.");
            return;
        }
        } catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        try
        {
            String s1 = s.substring(4, s.length() - 9);
            File file = (new File(_rootDir, URLDecoder.decode(s1, "UTF-8"))).getCanonicalFile();
            if(file.isDirectory())
            {
                File file1 = new File(file, "index.html");
                if(file1.exists() && !file1.isDirectory())
                    file = file1;
            }
            if(!file.toString().startsWith(_rootDir.toString()))
                sendError(bufferedoutputstream, 403, "Permission Denied.");
            else
            if(!file.exists())
                sendError(bufferedoutputstream, 404, "File Not Found.");
            else
            if(file.isDirectory())
            {
                if(!s1.endsWith("/"))
                    s1 = s1 + "/";
                File afile[] = file.listFiles();
                sendHeader(bufferedoutputstream, 200, "text/html", -1L, System.currentTimeMillis());
                String s3 = "Index of " + s1;
                bufferedoutputstream.write(("<html><head><title>" + s3 + "</title></head><body><h3>Index of " + s1 + "</h3><p>\n").getBytes());
                for(int i = 0; i < afile.length; i++)
                {
                    file = afile[i];
                    String s4 = file.getName();
                    String s5 = "";
                    if(file.isDirectory())
                        s5 = "&lt;DIR&gt;";
                    bufferedoutputstream.write(("<a href=\"" + s1 + s4 + "\">" + s4 + "</a> " + s5 + "<br>\n").getBytes());
                }

                bufferedoutputstream.write("</p><hr><p>SimpleWebServer  http://www.jibble.org/</p></body><html>".getBytes());
            } else
            {
                bufferedinputstream = new BufferedInputStream(new FileInputStream(file));
                String s2 = (String)SimpleWebServer.MIME_TYPES.get(SimpleWebServer.getExtension(file));
                if(s2 == null)
                    s2 = "application/octet-stream";
                sendHeader(bufferedoutputstream, 200, s2, file.length(), file.lastModified());
                byte abyte0[] = new byte[4096];
                int j;
                while((j = bufferedinputstream.read(abyte0)) != -1) 
                    bufferedoutputstream.write(abyte0, 0, j);
                bufferedinputstream.close();
            }
            bufferedoutputstream.flush();
            bufferedoutputstream.close();
        }
        catch(IOException ioexception)
        {
            if(bufferedinputstream != null)
                try
                {
                    bufferedinputstream.close();
                }
                catch(Exception exception) { }
        }
        return;
    }

    private File _rootDir;
    private Socket _socket;
}
